package com.cooper.scraper.show;

import com.cooper.android.comm.Resource;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public abstract class FanartAdapter<T extends Resource> extends BaseAdapter {
	
	
	public static class FanartAdapterOperHandler extends Handler{
		
		private FanartAdapter<?> adapter;
		
		
		public FanartAdapter<?> getAdapter() {
			return adapter;
		}


		public void setAdapter(FanartAdapter<?> adapter) {
			this.adapter = adapter;
		}


		@Override
		public void handleMessage(Message msg){
			if (msg.what == REFRESH_ADAPTER_MESSAGE && adapter != null){
				adapter.refresh();
			}
		}
		
	};
	
	public static final int REFRESH_ADAPTER_MESSAGE = 11;
	
	protected FanartsShow<T> fanartsShow;
	
	protected AdapterDataChangedListener dataChangedListener;

	public FanartsShow<T> getFanartViewAdapter() {
		return fanartsShow;
	}

	public void setFanartsShow(FanartsShow<T> fanartsShow) {
		this.fanartsShow = fanartsShow;
	}

	public abstract void destroy();
	
	public abstract void refresh();
	
	public abstract void setSearchKeyword(String keyword);
	
	protected abstract T getResource(int position);
	
	protected void onShowView(T resource, int position){
	}

	public void setDataChangedListener(AdapterDataChangedListener dataChangedListener) {
		this.dataChangedListener = dataChangedListener;
	}
	
	@Override
	public final View getView(int position, View convertView, ViewGroup parent) {
		T resource = getResource(position);
		onShowView(resource,position);
		return fanartsShow.getResourceView(resource, convertView,
				parent);
	}
	

}
